
//
//  CookViewController.m
//  Cooking
//
//  Created by ZhiPeng.Y on 15/8/29.
//  Copyright (c) 2015年 ZP.Y. All rights reserved.
//

#import "CookViewController.h"
#import "MyNavigationBar.h"
#import "AppMacro.h"
#import "StepTableViewCell.h"
#import "MaterialTableViewCell.h"
#import "CorrelationTableViewCell.h"
#import "AsyncGetConnection.h"
#import "StepModel.h"
#import "MaterialModel.h"
#import "RecommendModel.h"
#import "CorreiationModel.h"
#import "UIImageView+WebCache.h"
#import <MediaPlayer/MediaPlayer.h>
#import "MBProgressHUD.h"
#import "DiaryDataBase.h"

//枚举,判断点击状态
typedef NS_ENUM(NSInteger, click) {
    click_unSelected = 0,
    click_selected= 1
};

@interface CookViewController ()<MyNavigationBarDelegate, UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate, MBProgressHUDDelegate>

@property (retain, nonatomic) UITableView             *rootTableView;
@property (retain, nonatomic) UIView                  *headerView;
@property (retain, nonatomic) UIButton                *stepButton;
@property (retain, nonatomic) UIButton                *materialButton;
@property (retain, nonatomic) UIButton                *correlationButton;
@property (retain, nonatomic) UIButton                *goodButton;
@property (retain, nonatomic) NSMutableArray          *stepArray;
@property (retain, nonatomic) NSMutableArray          *materialArray;
@property (retain, nonatomic) NSMutableArray          *correlationArray;
@property (retain, nonatomic) NSMutableDictionary     *maDic;
@property (retain, nonatomic) UIImageView             *topImage;
@property (retain, nonatomic) MPMoviePlayerController *moviePlayer;
@property (retain, nonatomic) MBProgressHUD           *HUD;
@property (retain ,nonatomic) UIAlertView *alterView1;
@property (retain ,nonatomic) UIAlertView *alterView;
@property (nonatomic ,retain) UIButton *rightbutton;
@end

@implementation CookViewController

- (void)dealloc {
    [_rightbutton release];
    [_alterView release];
    [_alterView1 release];
    [_rootTableView release];
    [_headerView release];
    [_stepButton release];
    [_materialButton release];
    [_correlationButton release];
    [_goodButton release];
    [_stepArray release];
    [_maDic release];
    [_topImage release];
    [_moviePlayer release];
    [_HUD release];
    [_correlationArray release];
    [_nameStr release];
    [_myID release];
    [_recommendModel release];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.stepArray = [NSMutableArray array];
        self.materialArray = [NSMutableArray array];
        self.correlationArray = [NSMutableArray array];
        self.maDic = [NSMutableDictionary dictionary];
    }
    return self;
}

//在视频播放的过程中,如果切换至其他页面,设置视频暂停
- (void)viewWillDisappear:(BOOL)animated {
    [self.moviePlayer pause];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:nil];
    //为了避免,返回页面时,自定义navigationBar消失,系统navigationBar显示时,出现文字闪过不美观,将title设置为空字符串
    self.navigationItem.leftBarButtonItem.title = @"";
    self.navigationController.navigationBar.hidden = YES;
    self.navigationController.navigationBar.translucent = YES;    
    MyNavigationBar *mynavigationBar = [[MyNavigationBar alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 45)];
    mynavigationBar.delegate = self;
    if (_nameStr != nil) {
        mynavigationBar.titleLabel.text =_nameStr;
    }else{
        mynavigationBar.titleLabel.text =self.recommendModel.name;

    }
    
       [self.view addSubview:mynavigationBar];
    
    
    self.rightbutton = [[UIButton alloc] initWithFrame:CGRectMake(WIDTH - 30, 19,25, 25)];
    //    button.backgroundColor = [UIColor blackColor];
    [_rightbutton setBackgroundImage:[UIImage imageNamed:@"iconfont-shoucang-5"] forState:UIControlStateNormal];
    [_rightbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_rightbutton addTarget:self action:@selector(clickRightButton:) forControlEvents:UIControlEventTouchUpInside];
    [mynavigationBar addSubview:_rightbutton];
    
    RecommendModel *model = [DiaryDataBase selectWithRecommendId:self.recommendModel.vegetable_id];
    if (model.vegetable_id == nil ) {
      }else
      {
        [_rightbutton setBackgroundImage:[UIImage imageNamed:@"saved.png"] forState:UIControlStateNormal];
      }

    [_rightbutton release];
    [mynavigationBar release];
   
    [self creatSectionButton];
    [self createRootTableView];
    [self beginConnect];
}
//二选一,viewDidApper,或者 直接在button创建的时候直接给一个点击状态.
//- (void)viewDidAppear:(BOOL)animated
//{
//    [super viewDidAppear:animated];
//    _stepButton.selected = click_selected;
//    [self stepAction:_stepButton];
//}

//12196
#pragma mark - 
#pragma mark 网络数据解析
- (void)beginConnect {
    
    //制作步骤
    [AsyncGetConnection asyncGetConnectionWithURL:[NSString stringWithFormat:@"http://121.40.54.242:80/HandheldKitchen/api/vegetable/tblvegetable!getIntelligentChoice.do?vegetable_id=%@&type=2&phonetype=0&is_traditional=0", self.myID] parameters:nil block:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = [dic objectForKey:@"data"];
        for (NSDictionary *value in array) {
            StepModel *stepmodel = [[StepModel alloc] init];
           [stepmodel setValuesForKeysWithDictionary:value];
           [_stepArray addObject:stepmodel];
           [stepmodel release];
        }
        [_rootTableView reloadData];
        //制作请求数据量较大,作为异步请求,将加载等待隐藏,放在此处
        self.recommendModel.vegetable_id = self.myID;
//         self.recommendModel.name =
        [_HUD hide:YES afterDelay:1];
    }];
    
    //材料
    [AsyncGetConnection asyncGetConnectionWithURL:[NSString stringWithFormat:@"http://121.40.54.242:80/HandheldKitchen/api/vegetable/tblvegetable!getTblVegetables.do?vegetable_id=%@&phonetype=2&user_id=&is_traditional=0", self.myID] parameters:nil block:^(NSData *data) {
        
        NSDictionary *materialDic= [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *materialArray = [materialDic objectForKey:@"data"];
        for (NSDictionary *value in materialArray) {
            MaterialModel *materialModel = [[MaterialModel alloc] init];
            [materialModel setValuesForKeysWithDictionary:value];
            [_materialArray addObject:materialModel];
            [materialModel release];
        }

        [_topImage sd_setImageWithURL:[NSURL URLWithString:[[materialArray firstObject] valueForKey:@"imagePathThumbnails"]]];
        self.recommendModel.imagePathThumbnails = [[materialArray firstObject] valueForKey:@"imagePathThumbnails"];

        [_rootTableView reloadData];
    }];
    
    //材料细节
    [AsyncGetConnection asyncGetConnectionWithURL:[NSString stringWithFormat:@"http://121.40.54.242:80/HandheldKitchen/api/vegetable/tblvegetable!getIntelligentChoice.do?vegetable_id=%@&type=1&phonetype=0&is_traditional=0", self.myID] parameters:nil block:^(NSData *data) {
        NSDictionary *matDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *matArr = [matDic objectForKey:@"data"];
        self.maDic = [matArr firstObject];
    
        [_rootTableView reloadData];
    }];

    //相关常识
    [AsyncGetConnection asyncGetConnectionWithURL:[NSString stringWithFormat:@"http://121.40.54.242:80/HandheldKitchen/api/vegetable/tblvegetable!getIntelligentChoice.do?vegetable_id=%@&type=4&phonetype=0&is_traditional=0", self.myID] parameters:nil block:^(NSData *data) {
        NSDictionary *correlationDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *correlationArray = [correlationDic objectForKey:@"data"];
        NSDictionary *corDic = [correlationArray firstObject];
        CorreiationModel *correlationModel = [[CorreiationModel alloc] init];
        [correlationModel setValuesForKeysWithDictionary:corDic];
        [_correlationArray addObject:correlationModel];
        [correlationModel release];
        
        [_rootTableView reloadData];
    }];
}


//创建底层tableView
- (void)createRootTableView {
    
    self.rootTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 45, WIDTH, HEIGHT - 45 - 49) style:UITableViewStylePlain];
    _rootTableView.backgroundColor = [UIColor colorWithRed:250 / 255.0 green:242 / 255.0 blue:230 / 255.0 alpha:1];
    _rootTableView.delegate = self;
    _rootTableView.dataSource = self;
    _rootTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 3)];
    //    topView.backgroundColor = [UIColor redColor];
    
#warning 当前topImage为背景图片,上架时使用
    self.topImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, topView.frame.size.width, topView.frame.size.height)];
    _topImage.userInteractionEnabled = YES;
//    topView.backgroundColor = [UIColor purpleColor];
    [topView addSubview:_topImage];
    [_topImage release];
    _rootTableView.tableHeaderView = topView;
    
#warning 当前视频因包含logo水印,上架时,暂时抹除,上架后再进行更新
    UIButton *playButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
    playButton.backgroundColor = [UIColor whiteColor];
    playButton.alpha = 0.85;

    playButton.layer.cornerRadius = 40;
    playButton.center = CGPointMake(_topImage.frame.size.width / 2, _topImage.frame.size.height / 2);
    [playButton setBackgroundImage:[self imageWithName:@"iconfont-chaduibofang" type:@"png"] forState:UIControlStateNormal];
    [playButton addTarget:self action:@selector(playButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_topImage addSubview:playButton];
    [playButton release];
    
    [self.view addSubview:_rootTableView];
    [_rootTableView release];
    
    //加载等待
    self.HUD = [[MBProgressHUD alloc] init];
    _HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
    _HUD.delegate = self;
    _HUD.backgroundColor = [UIColor clearColor];
    _HUD.detailsLabelText = @"努力加载中...";
    _HUD.dimBackground = YES;
}

#pragma mark -
#pragma mark 自定义navigationBar返回按钮协议回调
- (void)clickButton:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark 收藏按钮

-(void)viewWillAppear:(BOOL)animated
{
    RecommendModel *model = [DiaryDataBase selectWithRecommendId:self.recommendModel.vegetable_id];
     RecommendModel *model1 = [DiaryDataBase selectWithRecommendId:self.myID];
    if (model.vegetable_id == nil && model1.vegetable_id == nil) {
       [_rightbutton setBackgroundImage:[UIImage imageNamed:@"iconfont-shoucang-5"] forState:UIControlStateNormal];
    }else
    {
        [_rightbutton setBackgroundImage:[UIImage imageNamed:@"saved.png"] forState:UIControlStateNormal];

    }
}


- (void)clickRightButton:(UIButton *)button {
    NSLog(@"收藏");
    RecommendModel *model = [DiaryDataBase selectWithRecommendId:self.recommendModel.vegetable_id];

    //如果查询为空,进行收藏,提示收藏成功
    if (model.vegetable_id == nil) {
        self.alterView1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];

         [DiaryDataBase insertWithRecommend:self.recommendModel];
        [button setBackgroundImage:[UIImage imageNamed:@"saved.png"] forState:UIControlStateNormal];
        [_alterView1 show];
        [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(cancel) userInfo:nil repeats:NO];
        [_alterView1 release];

    //否则已经收藏,进行取消收藏
    } else {
       self.alterView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已取消收藏" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [DiaryDataBase deleteWithRecommendId:self.recommendModel.vegetable_id];
         [button setBackgroundImage:[UIImage imageNamed:@"iconfont-shoucang-5"] forState:UIControlStateNormal];
        [_alterView show];
        [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(cancel) userInfo:nil repeats:NO];
        [_alterView release];
    }
}
 // 两秒后让alertView消失
-(void)cancel
{
    [self.alterView dismissWithClickedButtonIndex:0 animated:YES];
    [self.alterView1 dismissWithClickedButtonIndex:0 animated:YES];

}
#pragma mark -
#pragma mark rootTableView协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //判断当前是哪个buton被选中,返回不同cell的高度
    if (_stepButton.selected == click_selected) {
        //判断当前屏幕尺寸,返回不同cell的高度(适配)
        if ([UIScreen mainScreen].bounds.size.height == 667) {
            return (250 / HEIGHT) * HEIGHT;
        } else if ([UIScreen mainScreen].bounds.size.height == 568) {
            return (215 / HEIGHT) * HEIGHT;
            
        } else if ([UIScreen mainScreen].bounds.size.height == 736) {
            return (280 / HEIGHT) * HEIGHT;
        }
        return 190;
    } else if (_materialButton.selected == click_selected) {
        if ([UIScreen mainScreen].bounds.size.height == 667) {
            return HEIGHT - 45 - 49 - 40 - 180;
        } else if ([UIScreen mainScreen].bounds.size.height == 568) {
            return HEIGHT - 45 - 49 - 40 - 130;
            
        } else if ([UIScreen mainScreen].bounds.size.height == 736) {
            return HEIGHT - 45 - 49 - 40 - 180;
        }
        return 300;
    } else if (_correlationButton.selected == click_selected) {
        if ([UIScreen mainScreen].bounds.size.height == 667) {
            return HEIGHT - 45 - 49 - 40 - 140;
        } else if ([UIScreen mainScreen].bounds.size.height == 568) {
            return HEIGHT - 45 - 49 - 40 - 70;
            
        } else if ([UIScreen mainScreen].bounds.size.height == 736) {
            return HEIGHT - 45 - 49 - 40 - 140;
        }
        return 320;
    } else {
        return 0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 40;
    } else {
        return 20;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 40)];
    _headerView.backgroundColor = [UIColor whiteColor];
    //在headerView 创建 以后再把button 添加到视图上,button提前创建好,但不能添加视图,因为在协议里,每次切换cell重用的时候,button的点击状态会重置,出现错误.
    [_headerView addSubview:_goodButton];
    [_headerView addSubview:_materialButton];
    [_headerView addSubview:_stepButton];
    [_headerView addSubview:_correlationButton];

    UILabel *barLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 3, 8, 1, 22)];
    barLabel.backgroundColor = [UIColor grayColor];
    [_headerView addSubview:barLabel];
    [barLabel release];
    
    UILabel *secLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 3 * 2, 8, 1, 22)];
    secLabel.backgroundColor = [UIColor grayColor];
    [_headerView addSubview:secLabel];
    [secLabel release];
    
    return [_headerView autorelease];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //判断不同
    if (_stepButton.selected == click_selected) {
        return _stepArray.count;
    } else if (_materialButton.selected == click_selected) {
        return _materialArray.count;
    } else if (_correlationButton.selected == click_selected) {
        return _correlationArray.count;
    } else {
    return 0;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_stepButton.selected == click_selected) {
        static NSString *reuse = @"reuse";
        StepTableViewCell *stepCell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!stepCell) {
            stepCell = [[[StepTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse] autorelease];
            stepCell.backgroundColor = [UIColor colorWithRed:250 / 255.0 green:242 / 255.0 blue:230 / 255.0 alpha:1];
            stepCell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        StepModel *stepModel = [_stepArray objectAtIndex:indexPath.row];
        [stepCell sendValueWithModel:stepModel];
        return stepCell;
        
    } else if (_materialButton.selected == click_selected) {
        static NSString *materialReuse = @"materialReuse";
        MaterialTableViewCell *materialCell = [tableView dequeueReusableCellWithIdentifier:materialReuse];
        
        if (!materialCell) {
            materialCell = [[[MaterialTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:materialReuse] autorelease];
            materialCell.backgroundColor = [UIColor colorWithRed:250 / 255.0 green:242 / 255.0 blue:230 / 255.0 alpha:1];
            materialCell.selectionStyle = UITableViewCellSelectionStyleNone;
            
        }
        [materialCell.materialImage sd_setImageWithURL:[NSURL URLWithString:[_maDic objectForKey:@"imagePath"]] placeholderImage:[UIImage imageNamed:@"zhanwei_02.png"]];
        
        MaterialModel *materialModel = [_materialArray objectAtIndex:indexPath.row];
        [materialCell sendValueWithModel:materialModel];
        return materialCell;

    } else if (_correlationButton.selected == click_selected){
        static NSString *correlationReuse = @"CorrelationReuse";
        CorrelationTableViewCell *correlationCell = [tableView dequeueReusableCellWithIdentifier:correlationReuse];
        if (!correlationCell) {
            correlationCell = [[[CorrelationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:correlationReuse] autorelease];
            correlationCell.backgroundColor = [UIColor colorWithRed:250 / 255.0 green:242 / 255.0 blue:230 / 255.0 alpha:1];
            correlationCell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        CorreiationModel *correiationModel = [_correlationArray objectAtIndex:indexPath.row];
        [correlationCell sendValueWithModel:correiationModel];
        return correlationCell;

    } else {
        return nil;
    }
}


#pragma mark -
#pragma mark 按钮创建及点击方法
- (void)creatSectionButton {
    //先创建按钮,不添加到headerView上,等headerView创建之后,在添加,如果,在这里,直接添加,按钮将不能显示.
    
    //制作步骤
    self.stepButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, WIDTH / 3, 40)];
    [_stepButton setTitle:@"制作步骤" forState:UIControlStateNormal];
    [_stepButton setTitleColor:[UIColor colorWithRed:255 / 255.0 green:125 / 255.0 blue:67 / 255.0 alpha:1] forState:UIControlStateNormal];
    [_stepButton addTarget:self action:@selector(stepAction:) forControlEvents:UIControlEventTouchUpInside];
        _stepButton.selected = click_selected;
    [_stepButton release];
    //所需材料
    self.materialButton = [[UIButton alloc] initWithFrame:CGRectMake(_stepButton.frame.origin.x + _stepButton.frame.size.width, 0, WIDTH / 3, 40)];
    [_materialButton setTitle:@"所需材料" forState:UIControlStateNormal];
    [_materialButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_materialButton addTarget:self action:@selector(materialAction:) forControlEvents:UIControlEventTouchUpInside];
    [_materialButton release];
    //    _materialButton.selected = click_unSelected;
    //相关常识
    self.correlationButton = [[UIButton alloc] initWithFrame:CGRectMake(_materialButton.frame.origin.x + _materialButton.frame.size.width, 0, WIDTH / 3, 40)];
    [_correlationButton setTitle:@"相关知识" forState:UIControlStateNormal];
    [_correlationButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_correlationButton addTarget:self action:@selector(correlationAction:) forControlEvents:UIControlEventTouchUpInside];
    //    _correlationButton.selected = click_unSelected;
    [_correlationButton release];

}

//制作步骤点击事件
- (void)stepAction:(id)sender {
    UIButton *button = (UIButton *)sender;
    //遍历button父视图所有子视图.改变颜色
    NSArray *arr = [[button superview] subviews];
    for (UIButton *bu in arr) {
        if ([bu isKindOfClass:[UIButton class]]) {
            [bu setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        }
    }
    
    [button setTitleColor:[UIColor colorWithRed:255 / 255.0 green:125 / 255.0 blue:67 / 255.0 alpha:1] forState:UIControlStateNormal];
    NSLog(@"制作步骤");
    button.selected = click_selected;
    _materialButton.selected = click_unSelected;
    _correlationButton.selected = click_unSelected;
//    _goodButton.selected = click_unSelected;
    [_rootTableView reloadData];
    
}

//所需材料点击事件
- (void)materialAction:(id)sender {
        NSLog(@"所需材料");
    UIButton *button = (UIButton *)sender;
    NSArray *arr = [[button superview] subviews];
    for (UIButton *bu in arr) {
        if ([bu isKindOfClass:[UIButton class]]) {
            [bu setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        }
    }
    
    [button setTitleColor:[UIColor colorWithRed:255 / 255.0 green:125 / 255.0 blue:67 / 255.0 alpha:1] forState:UIControlStateNormal];
    button.selected = click_selected;
    _stepButton.selected = click_unSelected;
    _correlationButton.selected = click_unSelected;
//    _goodButton.selected = click_unSelected;
    [_rootTableView reloadData];
}

//相关知识
- (void)correlationAction:(id)sender {
    NSLog(@"相关知识");
    UIButton *button = (UIButton *)sender;
    NSArray *arr = [[sender superview] subviews];
    for (UIButton *bu in arr) {
        if ([bu isKindOfClass:[UIButton class]]) {
            [bu setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        }
    }
    
    [button setTitleColor:[UIColor colorWithRed:255 / 255.0 green:125 / 255.0 blue:67 / 255.0 alpha:1] forState:UIControlStateNormal];
    button.selected = click_selected;
    _stepButton.selected = click_unSelected;
    _materialButton.selected = click_unSelected;
//    _goodButton.selected = jjclick_unSelected;
    [_rootTableView reloadData];
}

#pragma mark -
#pragma mark 播放按钮事件

- (void)playButtonAction:(id)sender {
     NSLog(@"开始播放");
    [self.moviePlyer play];
    [self addNotifictication];
}

- (NSURL *)getNetWorkUrl {
    NSString *urlStr = [[_materialArray firstObject] valueForKey:@"productionProcessPath"];
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:urlStr];
    return url;
}

- (MPMoviePlayerController *)moviePlyer {
    if (!_moviePlayer) {
        NSURL *url = [self getNetWorkUrl];
        _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
        _moviePlayer.view.frame = CGRectMake(0, 0, WIDTH, _topImage.frame.size.height);
        _moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        [_topImage addSubview:_moviePlayer.view];
    }
    return _moviePlayer;
}

/*
 *  添加通知中心监控媒体播放控制器状态
 */
- (void)addNotifictication{
    NSNotificationCenter *notificationCenter=[NSNotificationCenter defaultCenter];
    [notificationCenter addObserver:self selector:@selector(mediaPlayerPlaybackStateChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayer];
    [notificationCenter addObserver:self selector:@selector(mediaPlayerPlaybackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
}

/* 
 *  播放状态改变，注意播放完成时的状态是暂停
 *
 *  @param notification 通知对象
 */
- (void)mediaPlayerPlaybackStateChange:(NSNotification *)notification{
    switch (self.moviePlayer.playbackState) {
        case MPMoviePlaybackStatePlaying:
            NSLog(@"正在播放...");
            break;
        case MPMoviePlaybackStatePaused:
            NSLog(@"暂停播放.");
            break;
        case MPMoviePlaybackStateStopped:
            NSLog(@"停止播放.");
            break;
        default:
            NSLog(@"播放状态:%ld",self.moviePlayer.playbackState);
            break;
    }
}

/*
 *  播放完成
 *
 *  @param notification 通知对象
 */
- (void)mediaPlayerPlaybackFinished:(NSNotification *)notification{
    NSLog(@"播放完成.%ld",self.moviePlayer.playbackState);
}


#pragma mark - 
#pragma mark  图片命名
- (UIImage *)imageWithName:(NSString *)imageName type:(NSString *)imageType {
    NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:imageType];
    UIImage *image = [UIImage imageWithContentsOfFile:path];
    return image;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
